//
//  FlyInputView.m
//  MedicalProject
//
//  Created by lifayu on 16/12/15.
//  Copyright © 2016年 贵阳飞云科技有限公司. All rights reserved.
//

#import "FlyInputView.h"

IB_DESIGNABLE
@implementation FlyInputView
@synthesize text = _text;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self _setupSubviews];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        [self _setupSubviews];
    }
    return self;
}

- (void)_setupSubviews
{
    _imageView = [[UIImageView alloc] init];
    _imageView.backgroundColor = kClearColor;
    _imageView.image = self.normalImage;
    [self addSubview:_imageView];
    
    self.textFiled = [[UITextField alloc] init];
    self.textFiled.backgroundColor = kClearColor;
    [self.textFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.textFiled.delegate = self;
    [self addSubview:self.textFiled];
    
    _lineView = [[UIView alloc] init];
    

    _lineView.backgroundColor = self.normalColor;
    [self addSubview:_lineView];
    
}

- (void)setTextFont:(CGFloat)textFont
{
    _textFont = textFont;
    if(textFont > 0)
    {
        self.textFiled.font = [UIFont systemFontOfSize:self.textFont];
    }
}


- (void)setText:(NSString *)text
{
    _text = text;
    if(!FLYStringIsEmpty(_text))
    {
        self.textFiled.text = text;
    }
}

- (NSString *)text
{
    return self.textFiled.text;
}

- (void)setPalceHolder:(NSString *)palceHolder
{
    _palceHolder = palceHolder;
    if(!FLYStringIsEmpty(_palceHolder))
    {
        self.textFiled.placeholder = palceHolder;
        if(_palceHolderColor)
        {
            self.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_palceHolder attributes:@{NSForegroundColorAttributeName:_palceHolderColor}];
        }
    }
}

- (void)setTextColor:(UIColor *)textColor
{
    _textColor = textColor;
    if(_textColor)
    {
        self.textFiled.textColor = textColor;
    }
}

- (void)setPalceHolderColor:(UIColor *)palceHolderColor
{
    _palceHolderColor = palceHolderColor;
    if(_palceHolderColor)
    {
        if(!FLYStringIsEmpty(_palceHolder))
        {
           self.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_palceHolder attributes:@{NSForegroundColorAttributeName:_palceHolderColor}];
        }
    }
}

- (void)setIsSecurity:(BOOL)isSecurity
{
    _isSecurity = isSecurity;
    self.textFiled.secureTextEntry = isSecurity;
}

- (CGFloat)normalLineSpace
{
    if(_normalLineSpace < 0.01f)
    {
        _normalLineSpace = 0.5f;
    }
    return _normalLineSpace;
}

- (CGFloat)editingLineSpace
{
    if(_editingLineSpace < self.normalLineSpace)
    {
        _editingLineSpace = 1.0f;
    }
    return _editingLineSpace;
}


- (void)setNormalColor:(UIColor *)normalColor
{
    _normalColor = normalColor;
    _lineView.backgroundColor = _normalColor;
}

- (void)setNormalImage:(UIImage *)normalImage
{
    _normalImage = normalImage;
    _imageView.image = normalImage;
    [self layoutIfNeeded];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [_imageView sizeToFit];
    _imageView.left = 0;
    _imageView.top = self.height/2 - _imageView.height/2;
    
    CGFloat textLeft = 0;
    if(_normalImage)
    {
        textLeft = _imageView.right + 10;
    }
    self.textFiled.frame = CGRectMake(textLeft, 0,self.width - (_imageView.right + 20),self.height);
    
    if(self.textFiled.enabled)
    {
        _lineView.frame = CGRectMake(0, self.height - self.normalLineSpace, self.width, self.normalLineSpace);
    }
    else
    {
        _lineView.frame = CGRectZero;
    }
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if(_editingImage)
    {
        _imageView.image = _editingImage;
    }
    if(_editingColor)
    {
        _lineView.backgroundColor = _editingColor;
    }
    
    if(self.textFiled.enabled)
    {
        _lineView.frame = CGRectMake(0, self.height - _editingLineSpace, self.width, _editingLineSpace);
    }
    else
    {
        _lineView.frame = CGRectZero;
    }
    [self layoutIfNeeded];
    
    if(_delegate && [_delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)])
    {
        return [_delegate textFieldShouldBeginEditing:textField];;
    }
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if(_normalImage)
    {
        _imageView.image = _normalImage;
    }
    if(_normalColor)
    {
        _lineView.backgroundColor = _normalColor;
    }
    _lineView.frame = CGRectMake(0, self.height - self.normalLineSpace, self.width, self.normalLineSpace);
    [self layoutIfNeeded];
    
    if(_delegate && [_delegate respondsToSelector:@selector(textFieldShouldEndEditing:)])
    {
        return [_delegate textFieldShouldEndEditing:textField];;
    }
    return YES;
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(_delegate && [_delegate respondsToSelector:@selector(textFieldShouldEndEditing:)])
    {
        [_delegate textFieldDidBeginEditing:textField];;
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(_delegate && [_delegate respondsToSelector:@selector(textFieldShouldEndEditing:)])
    {
        [_delegate textFieldDidEndEditing:textField];;
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason
{
    if(_delegate && [_delegate respondsToSelector:@selector(textFieldShouldEndEditing:)])
    {
        [_delegate textFieldDidEndEditing:textField reason:reason];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if(_delegate && [_delegate respondsToSelector:@selector(textFieldShouldEndEditing:)])
    {
        return [_delegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
    }
    if (string.length == 0) return YES;
    
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    if (_limitLength > 0 && existedLength - selectedLength + replaceLength > _limitLength)
    {
        return NO;
    }
    return YES;
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (_limitLength > 0 && textField.text.length > _limitLength)
    {
        textField.text = [textField.text substringToIndex:_limitLength];
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    if(_delegate && [_delegate respondsToSelector:@selector(textFieldShouldClear:)])
    {
        return [_delegate textFieldShouldClear:textField];;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(_delegate && [_delegate respondsToSelector:@selector(textFieldShouldReturn:)])
    {
        return [_delegate textFieldShouldReturn:textField];;
    }
    return YES;
}

@end
